package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理
 * @author wfyn
 * @date 2023/10/5
 */
@RestController(value = "adminDishController")
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
public class DishController {

    @Autowired
    DishService dishService;

    /**
     * 新增菜品
     * @param dishDTO 菜品DTO对象
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId")
    public Result save(@RequestBody DishDTO dishDTO)
    {


        dishService.saveWithFlavor(dishDTO);

        return Result.success("新增菜品成功");

    }
    /**
     * 菜品批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids)
    {


        dishService.deleteBatch(ids);

        return Result.success();
    }




    /**
     * 菜品分页查询
     * @param pageQueryDTO 菜品DTO对象
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO pageQueryDTO)
    {

        PageResult pageResult= dishService.pageQuery(pageQueryDTO);

        return Result.success(pageResult);

    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result update(@RequestBody DishDTO dishDTO)
    {

        dishService.updateWithFlavor(dishDTO);

        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){

        DishVO dishVO =dishService.getByIdWithFlavor(id);

        return  Result.success(dishVO);

    }

    /**
     * 起售,停售菜品
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("起售,停售菜品")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result<String> startOrStop(@PathVariable("status") Integer status, Long id){
        dishService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     *
     */

    @GetMapping("/list")
      public Result getDish(Long categoryId)
    {

        List<Dish> dishes=dishService.getDishByCategoryId(categoryId);

        return Result.success(dishes);
    }
}
